package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hamster.crmpre.config.LoginInterceptor;
import com.hamster.crmpre.mapper.CustomerMapper;
import com.hamster.crmpre.pojo.Customer;
import com.hamster.crmpre.service.CustomerQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Lizhipeng
 * @date ：Created in 2019/12/25 13:06
 * @description：客户信息查询实现类
 * @modified By：
 * @version: 1.0
 */
@Service
public class CustomerQueryServiceImpl implements CustomerQueryService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 获取一个部门客户的列表
     * @param department 部门
     * @return List<Customer>
     */
    @Override
    public List<Customer> list(String department) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department",department).orderByDesc("id");
        return customerMapper.selectList(queryWrapper);
    }

    /**
     * 获取个人负责的客户的列表
     * @param name 搜索的客户姓名内容
     * @return
     */
    @Override
    public List<Customer> listCustomerPersonal(String name) {
        String userId = LoginInterceptor.getLoginUser().getUserId();
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        //模糊查询姓名
        if (StringUtils.isNotBlank(name)){
            //queryWrapper.eq("developer_id",userId).like("name","%"+name+"%");
            queryWrapper.or().eq("principal_course_id",userId).like("name","%"+name+"%");
            queryWrapper.or().eq("principal_vip_id",userId).like("name","%"+name+"%");
            queryWrapper.or().eq("principal_company_id",userId).like("name","%"+name+"%");
        }else {
            queryWrapper.or().eq("principal_course_id",userId).or().eq("principal_vip_id",userId).or().eq("principal_company_id",userId);
        }
        queryWrapper.orderByDesc("id");
        List<Customer> list = customerMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 事业部查询 分公司开发的准客户的列表
     * @param companyId
     * @param name
     * @return
     */
    @Override
    public List<Customer> companyListCustomerPersonal(String companyId, String name) {
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("branch_company_id",companyId);
        if (StringUtils.isNotBlank(name)){
            //模糊查询姓名
            queryWrapper.like("name","%"+name+"%");
        }

        List<Customer> list = customerMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 事业部 查询分公司列表
     * @param userId
     * @param name
     * @return
     */
    @Override
    public List<Map<String, Object>> getCompanyList(String userId, String name) {
        List<Map<String, Object>> list = new ArrayList<>();
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("principal_company_id",userId);
        //模糊查询姓名
        //根据姓名过滤
        if (StringUtils.isNotBlank(name)){
            queryWrapper.like("name","%"+name+"%");
        }
        List<Customer> customerList = customerMapper.selectList(queryWrapper);
        for (Customer customer : customerList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id",customer.getId());
            map.put("companyName",customer.getName());
            map.put("companyType",customer.getBranchAttribute());//公司属性
            map.put("balance",customer.getBalance());//缴纳金额
            map.put("state",customer.getPayment());// 支付状态  0全款    1分期
            Integer number = customerMapper.queryCompanyCustomerCount(customer.getId());
            map.put("number",number);//分公司的客户人数
            map.put("bankNumber",customer.getBankNumber());
            map.put("alipay",customer.getAlipay());
            list.add(map);
        }


        return list;
    }

    /**
     * 获取自己负责的 VIP 列表
     * @param name 查询 姓名
     * @return
     */
    @Override
    public List<Customer> listCustomerPersonalVip(String name) {
        String userId = LoginInterceptor.getLoginUser().getUserId();
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        //模糊查询姓名
        if (StringUtils.isNotBlank(name)){
            queryWrapper.or().eq("principal_vip_id",userId).like("name","%"+name+"%");
        }else {
            queryWrapper.or().eq("principal_vip_id",userId).or();
        }
        List<Customer> list = customerMapper.selectList(queryWrapper);
        return list;
    }
}
